package com.xinjixian.webgame;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.content.Context.NOTIFICATION_SERVICE;

public class NotificationUtils {
    private Context mContext;
    private static NotificationUtils utils;
    private int REQUEST_CODE = 10010;

    private NotificationUtils(Context context) {
        mContext = context;
    }

    public static NotificationUtils getExample(Context context) {
        if (utils == null)
            utils = new NotificationUtils(context);
        return utils;
    }

    public void getNotificationUtils(String IMEI, String OAID, String deviceid) {
        IMEI = TextUtils.isEmpty(IMEI)?"":IMEI;
        OAID = TextUtils.isEmpty(OAID)?"":OAID;
        deviceid = TextUtils.isEmpty(deviceid)?"":deviceid;
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("imei", IMEI)
                .add("oaid", OAID)
                .add("androidid", deviceid)
                .build();
        Request request = new Request.Builder()
                .url("http://sy.piu4.com/cdcloud/box/notice")
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("okhttp", "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String value = response.body().string();
                Log.d("okhttp", "onResponse: " + value);
                try {
                    JSONObject object = new JSONObject(value);
                    String title = object.getString("title");
                    String details = object.getString("details");
                    Log.d("okhttp", "title: " + title+" details:"+details);
                    showNotificationUtils(title, details);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void showNotificationUtils(String title, String details) {
        Intent intent = new Intent(mContext, NotificationActivity.class);
        intent.putExtra("data", "15680");
        PendingIntent pendingIntent = PendingIntent.getActivity(
                mContext, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Log.e("123456", "111111");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 1. 创建一个通知(必须设置channelId)
            Context context = mContext.getApplicationContext();
            String channelId = "ChannelId"; // 通知渠道
            Notification notification = new Notification.Builder(mContext, "title")
                    .setChannelId(channelId)
                    .setSmallIcon(R.mipmap.logo_mojie)
                    .setContentTitle(title)
                    .setContentText(details)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .build();
            // 2. 获取系统的通知管理器(必须设置channelId)
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(
                    channelId,
                    "通知的渠道名称",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
            // 3. 发送通知(Notification与NotificationManager的channelId必须对应)
            notificationManager.notify(1, notification);
            Log.e("123456", "111112");
        } else {
            // 创建通知(标题、内容、图标)
            NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
            Notification notification = builder.setContentTitle(title)
                    .setContentText(details)
                    .setSmallIcon(R.mipmap.ic_65)
                    //.setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .build();
            // 创建通知管理器
            NotificationManager manager = (NotificationManager) mContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            // 发送通知
            manager.notify(2, notification);
            Log.e("123456", "111113");
        }
    }
}
